﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using GiveMeMenu.Service.ObjectModel;
using GiveMeMenu.Service.MessageContracts;
using GiveMeMenu.Service;
using ServiceProviderModels = GiveMeMenu.Interface.ServiceProvider.ObjectModel;


namespace GiveMeMenu.ServiceProvider.DataAccess
{
    /// <summary>
    /// GMMServiceAgent
    /// </summary>
    public class GMMServiceAgent : IGMMServiceAgent
    {
        private IGMMService GMMServiceClient;

        public GMMServiceAgent()
        {
            GMMServiceClient = new GiveMeMenu.Service.GMMService();
            RegisterAutoMapper();
        }

        private void RegisterAutoMapper()
        {
            Mapper.CreateMap<Dish, ServiceProviderModels.Dish>();
            Mapper.CreateMap<Order, ServiceProviderModels.Order>();
            Mapper.CreateMap<LuTable, ServiceProviderModels.LuTable>();
            Mapper.CreateMap<LuTypeDish, ServiceProviderModels.LuTypeDish>();
            Mapper.CreateMap<OrderInformation, ServiceProviderModels.OrderInformation>();
            Mapper.CreateMap<ServiceProviderModels.Dish, Dish>();
            Mapper.CreateMap<ServiceProviderModels.Order, Order>();
            Mapper.CreateMap<ServiceProviderModels.LuTypeDish, LuTypeDish>();
            Mapper.CreateMap<ServiceProviderModels.LuTable, LuTable>();
            Mapper.CreateMap<ServiceProviderModels.OrderInformation, OrderInformation>();

        }

        #region IGMMServiceAgent Members

        /// <summary>
        /// Gets all dishes.
        /// </summary>
        /// <returns></returns>
        public IList<Interface.ServiceProvider.ObjectModel.Dish> GetAllDishes()
        {
            try
            {
                var request = new RequestGetAllDishs();
                ResponseGetAllDishs response = GMMServiceClient.GetAllDishes(request);
                IList<ServiceProviderModels.Dish> result =
                    Mapper.Map<IList<Dish>, IList<ServiceProviderModels.Dish>>(response.Result);
                return result;
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Removes the order.
        /// </summary>
        /// <param name="OId">The O id.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool RemoveOrder(int OId)
        {
            try
            {
                var request = new RequestRemoveOrder()
                {
                    OId = OId
                };
                ResponseRemoveOrder response = GMMServiceClient.RemoveOrder(request);
                return response.Result;
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Removes the order information.
        /// </summary>
        /// <param name="OId">The O id.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool RemoveOrderInformation(int OId)
        {
            try
            {
                var request = new RequestRemoveOrderInformation()
                {
                    OId = OId
                };
                ResponseRemoveOrderInformation response = GMMServiceClient.RemoveOrderInformation(request);
                return response.Result;
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Gets the dish today.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public IList<Interface.ServiceProvider.ObjectModel.Dish> GetDishToday()
        {
            try
            {
                var request = new RequestGetDishToday();
                ResponseGetDishToday response = GMMServiceClient.GetDishToday(request);
                IList<ServiceProviderModels.Dish> result =
                    Mapper.Map<IList<Dish>, IList<ServiceProviderModels.Dish>>(response.Result);
                return result;
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Adds the dish.
        /// </summary>
        /// <param name="typeId">The type id.</param>
        /// <param name="name">The name.</param>
        /// <param name="price">The price.</param>
        /// <param name="description">The description.</param>
        /// <param name="orderDate">The order date.</param>
        /// <param name="discount">The discount.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool AddDish(ServiceProviderModels.Dish dish)
        {
            try
            {
                var request = new RequestAddDish()
                {
                    Dish = Mapper.Map<ServiceProviderModels.Dish, Dish>(dish)
                };
                ResponseAddDish response = GMMServiceClient.AddDish(request);
                return response.Result;
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Adds the order.
        /// </summary>
        /// <param name="tableId">The table id.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool AddOrder(ServiceProviderModels.Order order)
        {
            try
            {
                var request = new RequestAddOrder()
                {
                    Order = Mapper.Map<ServiceProviderModels.Order, Order>(order)
                };
                ResponseAddOrder response = GMMServiceClient.AddOrder(request);
                return response.Result;
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Adds the order information.
        /// </summary>
        /// <param name="orderId">The order id.</param>
        /// <param name="dishId">The dish id.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool AddOrderInformation(ServiceProviderModels.OrderInformation info)
        {
            try
            {
                var request = new RequestAddOrderInformation()
                {
                    OrderInformation = Mapper.Map<ServiceProviderModels.OrderInformation, OrderInformation>(info)
                };
                ResponseAddOrderInformation response = GMMServiceClient.AddOrderInformation(request);
                return response.Result;
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Addlus the table.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool AddluTable(ServiceProviderModels.LuTable luTable)
        {
            try
            {
                var request = new RequestAddluTable()
                {
                    LuTable = Mapper.Map<ServiceProviderModels.LuTable, LuTable>(luTable)
                };
                ResponseAddluTable response = GMMServiceClient.AddluTable(request);
                return response.Result;
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Addlus the type dish.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool AddluTypeDish(ServiceProviderModels.LuTypeDish luTypeDish)
        {
            try
            {
                var request = new RequestAddluTypeDish()
                {
                    LuTypeDish = Mapper.Map<ServiceProviderModels.LuTypeDish, LuTypeDish>(luTypeDish)
                };
                ResponseAddluTypeDish response = GMMServiceClient.AddluTypeDish(request);
                return response.Result;
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Getlus the tables.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public IList<Interface.ServiceProvider.ObjectModel.LuTable> GetluTables()
        {
            try
            {
                var request = new RequestGetluTables();
                ResponseGetluTables response = GMMServiceClient.GetluTables(request);
                return Mapper.Map<IList<LuTable>, IList<ServiceProviderModels.LuTable>>(response.Result);
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Getlus the type dish.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public IList<Interface.ServiceProvider.ObjectModel.LuTypeDish> GetluTypeDish()
        {
            try
            {
                var request = new RequestGetluTypeDish();
                ResponseGetluTypeDish response = GMMServiceClient.GetluTypeDish(request);
                return Mapper.Map<IList<LuTypeDish>, IList<ServiceProviderModels.LuTypeDish>>(response.Result);
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Gets the order information by order id.
        /// </summary>
        /// <param name="orderOId">The order O id.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public IList<Interface.ServiceProvider.ObjectModel.OrderInformation> GetOrderInformationByOrderId(int orderOId)
        {
            try
            {
                var request = new RequestGetOrderInformationByOrderId()
                {
                    OrderId = orderOId
                };
                ResponseGetOrderInformationByOrderId response = GMMServiceClient.GetOrderInformationByOrderId(request);
                return Mapper.Map<IList<OrderInformation>, IList<ServiceProviderModels.OrderInformation>>(response.Result);
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Removes the dish.
        /// </summary>
        /// <param name="oId">The o id.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool RemoveDish(int oId)
        {
            try
            {
                var request = new RequestRemoveDish()
                {
                    OId = oId
                };
                ResponseRemoveDish response = GMMServiceClient.RemoveDish(request);
                return response.Result;
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Removelus the table.
        /// </summary>
        /// <param name="oId">The o id.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool RemoveluTable(int oId)
        {
            try
            {
                var request = new RequestRemoveluTable()
                {
                    OId = oId
                };
                ResponseRemoveluTable response = GMMServiceClient.RemoveluTable(request);
                return response.Result;
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Removelus the type dish.
        /// </summary>
        /// <param name="oId">The o id.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool RemoveluTypeDish(int oId)
        {
            try
            {
                var request = new RequestRemoveluTypeDish()
                {
                    OId = oId
                };
                ResponseRemoveluTypeDish response = GMMServiceClient.RemoveluTypeDish(request);
                return response.Result;
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Updates the dish.
        /// </summary>
        /// <param name="oId"></param>
        /// <param name="typeId">The type id.</param>
        /// <param name="name">The name.</param>
        /// <param name="price">The price.</param>
        /// <param name="orderDate">The order date.</param>
        /// <param name="description">The description.</param>
        /// <param name="discount">The discount.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool UpdateDish(ServiceProviderModels.Dish dish)
        {
            try
            {
                var request = new RequestUpdateDish()
                {
                    Dish = Mapper.Map<ServiceProviderModels.Dish, Dish>(dish)
                };
                ResponseUpdateDish response = GMMServiceClient.UpdateDish(request);
                return response.Result;
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Updatelus the table.
        /// </summary>
        /// <param name="oId"></param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool UpdateluTable(ServiceProviderModels.LuTable luTable)
        {
            try
            {
                var request = new RequestUpdateluTable()
                {
                    LuTable = Mapper.Map<ServiceProviderModels.LuTable, LuTable>(luTable)
                };
                ResponseUpdateluTable response = GMMServiceClient.UpdateluTable(request);
                return response.Result;
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Updatelus the type dish.
        /// </summary>
        /// <param name="oId"></param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool UpdateluTypeDish(ServiceProviderModels.LuTypeDish luTypeDish)
        {
            try
            {
                var request = new RequestUpdateluTypeDish()
                {
                    LuTypeDish = Mapper.Map<ServiceProviderModels.LuTypeDish, LuTypeDish>(luTypeDish)
                };
                ResponseUpdateluTypeDish response = GMMServiceClient.UpdateluTypeDish(request);
                return response.Result;
            }
            catch
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Updates the order.
        /// </summary>
        /// <param name="oId">The o id.</param>
        /// <param name="tableId">The table id.</param>
        /// <param name="confirmOrder">if set to <c>true</c> [confirm order].</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public bool UpdateOrder(ServiceProviderModels.Order order)
        {
            try
            {
                var request = new RequestUpdateOrder()
                {
                    Order = Mapper.Map<ServiceProviderModels.Order, Order>(order)
                };
                ResponseUpdateOrder response = GMMServiceClient.UpdateOrder(request);
                return response.Result;
            }
            catch
            {
                throw new Exception();
            }
        }

        #endregion
    }
}
